/*
 * @Author: Binhuang4
 * @Date: 2019-05-31 15:37:42
 * @LastEditors: Binhuang4
 * @LastEditTime: 2019-05-31 16:03:26
 * @Description: file content
 */
package main

import (
	"fmt"
)

type Balance1 struct {
	Name    string
	Balance float32
}

func main() {
	defer runPanic()
	me := Balance1{"hwang", 20.22}
	zdh := Balance1{"zdh", 400.2}
	xyf := Balance1{"xyf", 600.2}
	save := atm1(true)
	save(&me, 1.78)
	save(&zdh, 17.8)
	save(&xyf, 21.8)
	fmt.Println(me)
	fmt.Println(zdh)
	fmt.Println(xyf)
	fmt.Println("----------------------")
	withDraw := atm1(false)
	withDraw(&me, 1.78)
	withDraw(&zdh, 17.8)
	withDraw(&xyf, 21.8)
	fmt.Println(me)
	fmt.Println(zdh)
	fmt.Println(xyf)
	fmt.Println("----------------------")
	withDraw(&me, 100)
	fmt.Println(me)
}

func atm1(inOrOut bool) func(b *Balance1, money float32) {
	if inOrOut {
		return func(b *Balance1, money float32) {
			b.Balance += money
		}
	} else {
		return func(b *Balance1, money float32) {
			if money > b.Balance {
				tmp := b.Balance
				b.Balance = 0
				panic(fmt.Sprintf("%s's Balance : %f,it's smaller than the withdraw money : %f", b.Name, tmp, money))
			} else {
				b.Balance -= money
			}
		}
	}
}

func runPanic() {
	if err := recover(); err != nil {
		fmt.Println(err)
	}
}
